package com.dahuyou.trigger.http;

import com.dahuyou.types.util.JwtUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 模拟受保护资源服务器
 */
@Controller
@RequestMapping("/protectedresource")
public class ProtectedResourceController {

    @RequestMapping("/giveMeFengJieYanZhao")
    @ResponseBody
    public void giveMeFengJieYanZhao(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        System.out.println("start accept post req, generate access_toen");
        String accessToken = request.getParameter("access_token");
        response.setContentType("text/html;charset=utf-8");
        try {
            boolean jwtValid = JwtUtils.isJwtValid(accessToken);
            // TODO 一些合法性判断，比如判断scope
            System.out.println("判断scope通过！");
            System.out.println("判断app_id信息通过！");
            if (jwtValid) response.getWriter().write("凤姐艳照,拿走不谢！！！多问一嘴，你要凤姐的艳照是为了减肥吗？？？");
            else response.getWriter().write("你这token不对啊，看不了凤姐艳照！！！");
        } catch (Exception e) {
            response.getWriter().write("sorry,发生了一些错误！！！");
        }
    }
}
